package com.link2room.aeriabots.controller.manager;

import com.link2room.aeriabots.dto.param.api.WxMenuApiParamDto;
import com.link2room.aeriabots.dto.result.api.WxMenuResDto;
import com.link2room.aeriabots.facade.IWxOfficialAccountsMenuFacadeService;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.link2room.aeriabots.constant.PermissionConstant.*;

/**
 * Created by LN on 2016/11/22.
 */
@Api(tags = "微信菜单")
@RestController
@RequestMapping("/manager/Menus")
public class WxMenuManagerController {
    @Resource
    private IWxOfficialAccountsMenuFacadeService wxOfficialAccountsMenuFacadeService;

    /**
     *
     * @param isHalt 是否禁用(删除更改这个字段)
     * @param inUse 在使用中
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "微信菜单获取", response = WxMenuResDto.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "isHalt", value = "是否禁用", required = false, paramType = "query"),
            @ApiImplicitParam(name = "inUse", value = "是否在使用中", required = false, paramType = "query"),
    })
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = wxMenus, name = "查看微信菜单")
    public BaseResultDto getMenus(
            @RequestParam(name = "isHalt",required = false) String isHalt,
            @RequestParam(name = "inUse" ,required = false)String inUse
    ) throws Exception {
        WxMenuApiParamDto parmDto = new WxMenuApiParamDto();
        parmDto.setIsHalt(isHalt);
        QueryResult<WxMenuResDto> menus = wxOfficialAccountsMenuFacadeService.getMenuResDto(parmDto);
        BaseResultDto resDto = new BaseResultDto();
        resDto.setResultCode(0);
        resDto.setQueryResult(menus);
        return resDto;
    }

    /**
     * 创建新的菜单
     *
     * @param parmDto
     * @return
     */
    @ApiOperation(value = "创建新的菜单")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = createMenus, name = "创建微信菜单")
    public @ResponseBody
    BaseResultDto createMenus(@RequestBody WxMenuApiParamDto parmDto) throws Exception {
        wxOfficialAccountsMenuFacadeService.createMenu(parmDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

    /**
     * 更新菜单
     */
    @ApiOperation(value = "更新菜单")
    @ApiImplicitParams(value = {

    })
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateMenus, name = "更新微信菜单")
    public @ResponseBody
    BaseResultDto updateMenus(@RequestBody WxMenuApiParamDto parmDto) throws Exception {
        wxOfficialAccountsMenuFacadeService.updateMenu(parmDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }

    /**
     * 发布菜单
     *
     * @param menuCode
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "发布菜单")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "menuCode",paramType = "path")
    })
    @RequestMapping(value = "{menuCode}", method = RequestMethod.POST)
    @L2RPermissionResources(value = publishMenus, name = "发布菜单")
    public @ResponseBody
    BaseResultDto publish(@PathVariable String menuCode) throws Exception {
        WxMenuApiParamDto parmDto = new WxMenuApiParamDto();
        parmDto.setMenuCode(menuCode);
        wxOfficialAccountsMenuFacadeService.publishMenu(parmDto);
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        return resultDto;
    }
}
